//
//  MediaBookXMLParser.m
//  Dolphin
//
//  Created by Handymood on 11-5-25.
//  Copyright 2011 __Hanydmood__. All rights reserved.
//

#import "MediaBookXMLParser.h"

@implementation MediaBookXMLParser

@synthesize pagesArr;
@synthesize pagePropArr;
@synthesize gDataSet;
@synthesize upToolBarInfo;
@synthesize bottomIndexPage;
@synthesize isIPad;

-(void)startParsing:(NSString *) inXMLAddress
              finishMsg:(NSString *) inLoadOverMsg
{
    loadOverMSG = [[NSString alloc] initWithString:inLoadOverMsg];
    
    dataReceiveErr = FALSE;
    
    NSString *fileNamePrefix = [inXMLAddress  substringWithRange:NSMakeRange(0, [inXMLAddress  length] - 4)];
    NSString *fileNameSuffix = [inXMLAddress substringWithRange:NSMakeRange([inXMLAddress length]-3,3)];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:fileNamePrefix ofType:fileNameSuffix];
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    
    BOOL loadXmlFromWeb = FALSE;
    
    if([inXMLAddress length] > 11) 
    {
        NSString *httpSuffix = [inXMLAddress substringWithRange:NSMakeRange(0,7)];
        
        if([[httpSuffix lowercaseString] isEqualToString:@"http://"])
        {
            loadXmlFromWeb = YES;
            
            NSURL *url = [NSURL URLWithString:inXMLAddress];
            xmlDataBuffer = [[NSMutableData alloc] init];
            xmlLoadConnect = [[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self] retain];
            [xmlLoadConnect start];
        }
    }
    
    if (!loadXmlFromWeb)
    {
        xmlTextReaderPtr xmlreader = xmlReaderForMemory([xmlData bytes], 
                                                        [xmlData length], 
                                                        [path UTF8String], nil, 
                                                        (XML_PARSE_NOBLANKS | XML_PARSE_NOCDATA | XML_PARSE_NOERROR | XML_PARSE_NOWARNING));
        [self xmlParseProc:xmlreader];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    int statusCode = [((NSHTTPURLResponse *) response) statusCode];
    if (statusCode >= 400 && dataReceiveErr == FALSE)
    {
        UIAlertView *av= [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Web connection fail, please try later." delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
        [av show];
        dataReceiveErr = TRUE;
    }
    
    [xmlDataBuffer setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (dataReceiveErr) 
    {
        return;
    }
    
    [xmlDataBuffer appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (dataReceiveErr)
    {
        return;
    }
    
    xmlTextReaderPtr xmlreader = xmlReaderForMemory([xmlDataBuffer  bytes], 
                                                    [xmlDataBuffer  length], 
                                                    nil, nil, 
                                                    (XML_PARSE_NOBLANKS | XML_PARSE_NOCDATA | XML_PARSE_NOERROR | XML_PARSE_NOWARNING));
    [self xmlParseProc:xmlreader];
    
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    dataReceiveErr = TRUE;
    UIAlertView *av= [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Web connection fail, please try later." delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [av show];
}

-(void) xmlParseProc:(xmlTextReaderPtr) xmlreader
{
    isIPad = TRUE;
    
    if (!xmlreader) 
    {
        NSLog(@"xmlreader load failure");
        return;
    }
    
    //init the data for read
    pagesArr = [[NSMutableArray alloc] init];//array store the page's data.
    pagePropArr = [[NSMutableArray alloc] init];
    
    gDataSet = [[GlobalSet alloc] init];
    [gDataSet initDefaultSet];
    
    upToolBarInfo = [[UpToolBarInfo alloc] init];
    bottomIndexPage = [[IndexPageInfo alloc] init];
    
    [upToolBarInfo initDefaultSet];
    [bottomIndexPage initDefaultSet];
    
    NSMutableArray *layerArr = [[NSMutableArray alloc] init]; // the layers array for per pages
    SinglePageInfo *tempPageInfo = [[SinglePageInfo alloc] init];
    LayerInfo *tempLayerInfo = [[LayerInfo alloc] init];
    
    NSString *tempTagName = Nil;
    NSString *tempTagValue = Nil; 
    NSString *currentTag = Nil;
    char *tempData;
    int count = 0;
    int tag_count = 0;
    
    while (true) 
    {
        if (!xmlTextReaderRead(xmlreader))
        {    
            break;
        }
        
        //xml validate check
        count++; 
        if (count>100000) 
        {
            count = -1;
            break;
        }
        
        int curtype = xmlTextReaderNodeType(xmlreader);
        
        switch (curtype) 
        {
            case XML_READER_TYPE_ELEMENT:
                //We are starting an element
                tempData =  (char*)xmlTextReaderConstName(xmlreader);
                
                tempTagName = [NSString stringWithCString:tempData 
                                                 encoding:NSUTF8StringEncoding];
                
                if ([tempTagName isEqualToString:@"global"]) //init the global set data
                {
                    currentTag = @"global";
                    tag_count++;
                }
                else if([tempTagName isEqualToString:@"up_toolbar"])//up toolbar
                {
                    currentTag = @"up_toolbar";
                    tag_count++;
                }
                else if([tempTagName isEqualToString:@"bottom_index"])//init the index page set
                {
                    currentTag = @"bottom_index";
                    tag_count++;
                }
                else if([tempTagName isEqualToString:@"page"])//init the page set unit
                {
                    if(layerArr != Nil && [layerArr retainCount]>=1)
                    {
                        [layerArr release];
                    }
                    
                    layerArr = [[NSMutableArray alloc] init];
                    [pagesArr addObject:layerArr];
                    
                    //init page prop
                    tempPageInfo = [[SinglePageInfo alloc] init];
                    [tempPageInfo defaultValueInit];
                    [self preDefaultGlobalDataToSinglePage:tempPageInfo];
                    [pagePropArr addObject:tempPageInfo];
                    
                    currentTag = @"page";
                }
                else if([tempTagName isEqualToString:@"page_prop"])//init the page set unit
                {
                    currentTag = @"page_prop";
                }
                else if([tempTagName isEqualToString:@"layer"])//init the page set unit
                {
                    if(layerArr)
                    {
                        if(tempLayerInfo && [tempLayerInfo retainCount]>=1)
                        {
                            [tempLayerInfo release];
                        }
                        
                        tempLayerInfo =[[LayerInfo alloc] init];
                        [tempLayerInfo initDefaultSet];
                        [layerArr addObject:tempLayerInfo];
                    }
                    
                    currentTag = @"layer";
                }
                
                continue;
            case XML_READER_TYPE_TEXT: 
                tempData = (char*)xmlTextReaderConstValue(xmlreader);
                tempTagValue = [NSString stringWithCString:tempData
                                                  encoding:NSUTF8StringEncoding];
                
                if([currentTag isEqualToString:@"global"])
                {
                    if ([tempTagName isEqualToString:@"align"]) 
                    {
                        gDataSet.align = tempTagValue;
                    }
                    else if([tempTagName isEqualToString:@"album_path"])
                    {
                        gDataSet.albumListPath = tempTagValue;
                    }
                    else if ([tempTagName isEqualToString:@"auto_screen_fit"]) 
                    {
                        gDataSet.fit_with_screen = [tempTagValue boolValue];
                    }
                    else if ([tempTagName isEqualToString:@"page_width"]) 
                    {
                        gDataSet.page_width = [tempTagValue intValue];
                    }
                    else if ([tempTagName isEqualToString:@"page_height"]) 
                    {
                        gDataSet.page_height = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"fullscreen_mode"])
                    {
                        gDataSet.fullscreen_mode = [tempTagValue boolValue];
                    }
                    else if ([tempTagName isEqualToString:@"status_bar_hidden"]) 
                    {
                        gDataSet.status_bar_hidden = [tempTagValue boolValue];
                    }
                    else if ([tempTagName isEqualToString:@"fit_page_size"]) 
                    {
                        gDataSet.fit_page_size = [tempTagValue boolValue];
                    }
                    else if([tempTagName isEqualToString:@"active_page_num"])
                    {
                        gDataSet.active_page_num = [tempTagValue intValue];
                        
                        if (gDataSet.active_page_num <1)
                        {
                            gDataSet.active_page_num = 1;
                        }
                        else if(gDataSet.active_page_num>20)
                        {
                            gDataSet.active_page_num = 20;
                        }
                    }
                    else if( [tempTagName isEqualToString:@"showtoolbar_at_init" ])
                    {
                        gDataSet.show_toolbar_at_init = [tempTagValue boolValue];
                    }
                    else if( [tempTagName isEqualToString:@"bg_img"])
                    {
                        gDataSet.bg_img = tempTagValue;
                    }   
                    else if([tempTagName isEqualToString:@"bg_img_iphone"])
                    {
                        gDataSet.bg_img_iphone = tempTagValue;
                    }
                    else if([tempTagName isEqualToString:@"bg_snd_link"])
                    {
                        gDataSet.bg_snd_link = tempTagValue; 
                    }
                    else if([tempTagName isEqualToString:@"bg_snd_play_at_init" ])
                    {
                        gDataSet.bg_snd_play_at_init = [tempTagValue boolValue];
                    }
                    else if([tempTagName isEqualToString:@"bg_color"])
                    {
                        gDataSet.bg_color = tempTagValue;
                    }
                    else if([tempTagName isEqualToString:@"auto_play_on"])
                    {
                        gDataSet.auto_play_on = [tempTagValue boolValue];
                    }   
                    else if([tempTagName isEqualToString:@"auto_play_at_init"])
                    {
                        gDataSet.auto_play_at_init = [tempTagValue boolValue];
                    }
                    else if([tempTagName isEqualToString:@"auto_play_time_tick"])
                    {
                        gDataSet.auto_play_time_tick = [tempTagValue floatValue];
                    }
                    else if([tempTagName isEqualToString:@"single_page_zoom_min"])
                    {
                        gDataSet.single_page_zoom_min = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"single_page_zoom_max"])
                    {
                        gDataSet.single_page_zoom_max = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"landscape_two_page"])
                    {
                        gDataSet.landscape_two_page = [tempTagValue boolValue];
                    }
                }
                else if([currentTag isEqualToString:@"up_toolbar"])
                {
                    if ([tempTagName isEqualToString:@"height"]) 
                    {
                        upToolBarInfo.height = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"height_iphone"]) 
                    {
                        upToolBarInfo.height_iphone = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"home_link"])
                    {
                        upToolBarInfo.home_link = tempTagValue;
                    }
                    else if ([tempTagName isEqualToString:@"facebook_link"])
                    {
                        upToolBarInfo.facebook_link = tempTagValue;
                    }
                    else if ([tempTagName isEqualToString:@"twitter_link"])
                    {
                        upToolBarInfo.twitter_link = tempTagValue;
                    }
                    else if([tempTagName isEqualToString:@"page_index_display"])
                    {
                        upToolBarInfo.page_index_display = [tempTagValue boolValue];
                    }
                    else if([tempTagName isEqualToString:@"visible"])
                    {
                        upToolBarInfo.visible = [tempTagValue boolValue];
                    }
                    else if([tempTagName isEqualToString:@"link_type"])
                    {
                        upToolBarInfo.openType = [tempTagValue intValue];
                    }
                }
                else if([currentTag isEqualToString:@"bottom_index"])
                {                 
                    if ([tempTagName isEqualToString:@"grid_mode"]) 
                    {
                        bottomIndexPage.grid_mode_on = [tempTagValue boolValue];
                    }
                    else if ([tempTagName isEqualToString:@"bottom_line_mode"]) 
                    {
                        bottomIndexPage.bottom_line_mode_on = [tempTagValue boolValue];
                    }
                    else if ([tempTagName isEqualToString:@"units"])
                    {                        
                        bottomIndexPage.unitArr = [[NSMutableArray alloc] init];
                        
                        NSArray *tempArr = [tempTagValue componentsSeparatedByString:@","];
                        
                        for (int i = 0; i<[tempArr count]; i++) 
                        {
                            [bottomIndexPage.unitArr addObject:[[NSString alloc] initWithString:[tempArr objectAtIndex:i]]];
                        }
                    }
                    else if([tempTagName isEqualToString:@"target_page_id"])
                    {
                        bottomIndexPage.targetPageIdArr = [[NSMutableArray alloc] init];
                        
                        NSArray *tempArr = [tempTagValue componentsSeparatedByString:@","];
                        
                        for (int i = 0; i<[tempArr count]; i++) 
                        {
                            [bottomIndexPage.targetPageIdArr addObject:[[NSString alloc] initWithString:[tempArr objectAtIndex:i]]];
                        }
                    }
                    else if ([tempTagName isEqualToString:@"unit_width"]) 
                    {
                        bottomIndexPage.unit_width = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"unit_height"]) 
                    {
                        bottomIndexPage.unit_height = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"unit_width_iphone"]) 
                    {
                        bottomIndexPage.unit_width_iphone = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"unit_height_iphone"]) 
                    {
                        bottomIndexPage.unit_height_iphone = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"unit_between_width"]) 
                    {
                        bottomIndexPage.unit_between_width = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"unit_between_height"]) 
                    {
                        bottomIndexPage.unit_between_height = [tempTagValue floatValue];
                    }                    
                    else if ([tempTagName isEqualToString:@"start_unit_offset_x"]) 
                    {
                        bottomIndexPage.start_unit_offset_x = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"start_unit_offset_y"]) 
                    {
                        bottomIndexPage.start_unit_offset_y = [tempTagValue floatValue];
                    }                     
                    else if ([tempTagName isEqualToString:@"grid_column_num"]) 
                    {
                        bottomIndexPage.grid_column_num = [tempTagValue intValue];
                    }
                    else if ([tempTagName isEqualToString:@"grid_column_autoalign"]) 
                    {
                        bottomIndexPage.grid_column_autoalign = [tempTagValue boolValue];
                    }
                    else if ([tempTagName isEqualToString:@"unit_ratio_mode"]) 
                    {
                        bottomIndexPage.unit_ratio_mode = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"visible"])
                    {
                        bottomIndexPage.visible = [tempTagValue boolValue];
                    }
                }
                else if([currentTag isEqualToString:@"page_prop"])
                {
                    if([tempTagName isEqualToString:@"single_page_zoom_min"])
                    {
                        tempPageInfo.single_page_zoom_min = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"single_page_zoom_max"])
                    {
                        tempPageInfo.single_page_zoom_max = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"scroll_width"])
                    {
                        tempPageInfo.scroll_width = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"scroll_height"])
                    {
                        tempPageInfo.scroll_height = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"page_id"])
                    {
                        tempPageInfo.page_id = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"fit_page_size"])
                    {
                        tempPageInfo.resizeMode = [tempTagValue intValue];
                    }
                }
                else if([currentTag isEqualToString:@"layer"] && tempLayerInfo)
                {
                    if ([tempTagName isEqualToString:@"type"]) 
                    {
                        tempLayerInfo.type = tempTagValue;
                    }
                    else if ([tempTagName isEqualToString:@"src"]) 
                    {
                        tempLayerInfo.resPath = tempTagValue; 
                    }
                    else if([tempTagName isEqualToString:@"imgarr"]) 
                    {
                        tempLayerInfo.imgArr =[[NSMutableArray alloc] init];
                        
                        NSArray *tempArr = [tempTagValue componentsSeparatedByString:@","];
                        
                        for (int i = 0; i<[tempArr count]; i++) 
                        {
                            [tempLayerInfo.imgArr addObject:[[NSString alloc] initWithString:[tempArr objectAtIndex:i]]];
                        }
                    }
                    else if([tempTagName isEqualToString:@"target_album_xml"])
                    {
                        tempLayerInfo.target_album_xml = tempTagValue;
                    }
                    else if ([tempTagName isEqualToString:@"x"]) 
                    {
                        tempLayerInfo.x = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"y"]) 
                    {
                        tempLayerInfo.y = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"offx"]) 
                    {
                        tempLayerInfo.x += [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"offy"]) 
                    {
                        tempLayerInfo.y += [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"width"]) 
                    {
                        tempLayerInfo.width = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"height"]) 
                    {
                        tempLayerInfo.height = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"imgwidth"]) 
                    {
                        tempLayerInfo.imgWidth = [tempTagValue floatValue];
                    }
                    else if ([tempTagName isEqualToString:@"imgheight"]) 
                    {
                        tempLayerInfo.imgHeight = [tempTagValue floatValue];
                    }
                    else if([tempTagName isEqualToString:@"autoplay"]) 
                    {
                        tempLayerInfo.autoPlay = [tempTagValue boolValue];
                    }
                    else if([tempTagName isEqualToString:@"loop"]) 
                    {
                        tempLayerInfo.isLoop = [tempTagValue boolValue];
                    }
                    else if([tempTagName isEqualToString:@"showbutton"]) 
                    {
                        tempLayerInfo.showButton = [tempTagValue boolValue];
                    }
                    else if([tempTagName isEqualToString:@"originalsize"])
                    {
                        tempLayerInfo.useOriginalSize = [tempTagValue boolValue];
                    }
                    else if([tempTagName isEqualToString:@"ratiomode"]) 
                    {
                        tempLayerInfo.ratioMode = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString: @"target_page_id"])
                    {
                        tempLayerInfo.target_page_id = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"scroll_width"])
                    {
                        tempLayerInfo.scroll_width = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"scroll_height"])
                    {
                        tempLayerInfo.scroll_height = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"style"])
                    {
                        tempLayerInfo.style = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"msg"])
                    {
                        tempLayerInfo.msg = tempTagValue;
                    }
                    else if([tempTagName isEqualToString:@"value"])
                    {
                        tempLayerInfo.value = tempTagValue;
                    }
                    else if([tempTagName isEqualToString:@"target_video_path"])
                    {
                        tempLayerInfo.target_video_path = tempTagValue;
                    }
                    else if([tempTagName isEqualToString:@"fade"])
                    {
                        tempLayerInfo.fade = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"startOffX"])
                    {
                        tempLayerInfo.startOffX = [tempTagValue floatValue];
                    }
                    else if([tempTagName isEqualToString:@"startOffY"])
                    {
                        tempLayerInfo.startOffY = [tempTagValue floatValue];
                    }
                    else if([tempTagName isEqualToString:@"startAlpha"])    
                    {
                        tempLayerInfo.startAlpha = [tempTagValue floatValue];
                    }
                    else if([tempTagName isEqualToString:@"delay"])
                    {
                        tempLayerInfo.delay = [tempTagValue floatValue];
                    }
                    else if([tempTagName isEqualToString:@"duration"])
                    {
                        tempLayerInfo.duration = [tempTagValue floatValue];
                    }
                    else if([tempTagName isEqualToString:@"transitionType"])
                    {
                        tempLayerInfo.transitionType = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"aniprefix"])
                    {
                        tempLayerInfo.aniprefix = tempTagValue ;
                    }      
                    else if([tempTagName isEqualToString:@"anisuffix"])
                    {
                        tempLayerInfo.anisuffix = tempTagValue;
                    }    
                    else if([tempTagName isEqualToString:@"ani"])
                    {
                        tempLayerInfo.ani = [tempTagValue intValue];
                    }    
                    else if([tempTagName isEqualToString:@"anistartindex"])
                    {
                        tempLayerInfo.anistartindex = [tempTagValue intValue];
                    }    
                    else if([tempTagName isEqualToString:@"aniendindex"])
                    {
                        tempLayerInfo.aniendindex = [tempTagValue intValue];
                    }    
                    else if([tempTagName isEqualToString:@"anirepeat"])
                    {
                        tempLayerInfo.anirepeat = [tempTagValue intValue];
                    }    
                    else if([tempTagName isEqualToString:@"aniduration"])
                    {
                        tempLayerInfo.aniduration = [tempTagValue floatValue];
                    }
                    else if([tempTagName isEqualToString:@"discrete"])
                    {
                        tempLayerInfo.discrete = [tempTagValue intValue];
                    }
                    else if([tempTagName isEqualToString:@"hit_img"])
                    {
                        tempLayerInfo.hit_img = tempTagValue ;
                    }
                }
                
                tempTagValue = nil;
                tempTagName = nil;
                continue;
                
            default: 
                continue;
        }
    }
    
    xmlreader = Nil;

    if(count == -1 || tag_count <3 )
    {
        UIAlertView *av= [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"Data load error. Please try later." delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
        [av show];
    }
    else
    {
        int page_count = 0;
        NSMutableDictionary *pageIdDict = [[NSMutableDictionary alloc] init];
        
        for (int i=0; i<[pagePropArr count]; i++)
        {
            if([[pagePropArr objectAtIndex:i] page_id] == -1)
            {
                [pageIdDict setValue:@"1" forKey:[NSString stringWithFormat:@"%d",page_count]];
                page_count++;
            }
            else if([[pagePropArr objectAtIndex:i] page_id]>=0)
            {
                NSNumber *num = [NSNumber numberWithInt:[[pagePropArr objectAtIndex:i] page_id]];
                NSString *id = [pageIdDict objectForKey:[NSString stringWithFormat:@"%@",num]];
                
                if(id &&[ id isEqualToString:@"1"]) // if page already exists
                {
                    //do nothing
                }
                else
                {
                    [pageIdDict setValue:@"1" forKey:[NSString stringWithFormat:@"%d",page_count]];
                    page_count++;
                }
            }
        }
        
        gDataSet.page_num = page_count;    
        
        [[NSNotificationCenter defaultCenter] postNotificationName:loadOverMSG object:self];
    }
    
}

-(void) preDefaultGlobalDataToSinglePage:(SinglePageInfo *) tempPageInfo
{
    if (tempPageInfo != Nil) 
    {
        tempPageInfo.single_page_zoom_min = gDataSet.single_page_zoom_min;
        tempPageInfo.single_page_zoom_max = gDataSet.single_page_zoom_max;
        
        tempPageInfo.ori_page_width = gDataSet.page_width;
        tempPageInfo.ori_page_height = gDataSet.page_height;
        
        tempPageInfo.resizeMode = gDataSet.fit_page_size;
    }
}

-(void) makeDataCompatibleWithIPhone
{
    //judge whether is a iphone
    CGRect screen_rect = [[UIScreen mainScreen] bounds];
    if(screen_rect.size.width >= 768 || screen_rect.size.height>=768)
    {
        //ipad
        isIPad = TRUE;
    }
    else//iphone
    {
        gDataSet.bg_img = gDataSet.bg_img_iphone;
        upToolBarInfo.height = upToolBarInfo.height_iphone;
        bottomIndexPage.unit_width = bottomIndexPage.unit_width_iphone;
        bottomIndexPage.unit_height = bottomIndexPage.unit_height_iphone;
        isIPad = FALSE;
    }
}

-(void)reset
{
    if(xmlLoadConnect)
    {
        [xmlLoadConnect cancel];
        [xmlLoadConnect release];
    }
    
    if (xmlDataBuffer) 
    {
        [xmlDataBuffer setLength:0];
        [xmlDataBuffer release];
    }
    
    if (pagesArr) 
    {
        [pagesArr removeAllObjects];
        [pagesArr release];
    }
    
    if (pagePropArr) 
    {
        [pagePropArr removeAllObjects];
        [pagePropArr release];
    }
    
    if (gDataSet)
    {
        [gDataSet reset];
        [gDataSet release];
    }
    
    if(upToolBarInfo)
    { 
        [upToolBarInfo reset];
        [upToolBarInfo release];
    }
    
    if(bottomIndexPage)
    {
        [bottomIndexPage reset];
        [bottomIndexPage release];
    }
}

@end
